#include "car_product.h"
#include "datebase.h"
Car_Product::Car_Product(datebase *db,QObject *parent) :
    QThread(parent)
{
    this->pt_mydb=db;
}

void Car_Product::run()
{
    //qDebug()<<"=====下载固定车Start=====";
    down_car();
    //qDebug()<<"=====下载固定车End=======";
}

void Car_Product::down_car()
{
    QEventLoop loop;
    QNetworkAccessManager manager;
    max_updated_at=pt_mydb->select_updateTime(&pt_mydb->sqlite_db);
    QString str=cardown(max_updated_at);
    QUrl url(str);
    //qDebug()<<"down_car()max_updated_at:"<<max_updated_at<<str;
    QNetworkRequest request(url);
    QNetworkReply *reply=manager.get(request);
    connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    loop.exec();
    Producer_WebCarMessage(reply);
}

bool Car_Product::Producer_WebCarMessage(QNetworkReply *reply)
{
    QVariant statusCodeV =reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    QVariant redirectionTargetUrl =reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    //qDebug()<<"ProWebCardown"<<statusCodeV.toString ()<<redirectionTargetUrl.toString ();
    if (reply->error() == QNetworkReply::NoError)
    {
        QTextCodec *codec = QTextCodec::codecForName("UTF-8");
        QString str = codec->toUnicode(reply->readAll());
       // qDebug()<<"***strProWebCar***"<<str;
        if(str.length()<10)
        {
            emit MsgSignal(max_updated_at);
            return false;
        }
        QScriptEngine engine;
        QScriptValue sc = engine.evaluate("value="+str);
        QScriptValueIterator it(sc.property("result"));
        while (it.hasNext())
        {
            QString Id,carNo,stop_date,start_date,person_name,person_tel,status;
            QString car_type,fee_type,begin_time,end_time,allow_count,allow_park_time,vehicle_type,created_at,updated_at;
            ticketCheck_t check_car;
            webcar_down webCarInfo;

            it.next();
            if(!it.value().property("id").toString().isEmpty())
            {
                Id=it.value().property("id").toString();
                webCarInfo.id_car=Id;
              //  qDebug()<<"webCarInfo.car_id"<<webCarInfo.id_car;
            }
            if(!it.value().property("car_no").toString().isEmpty())
            {
                carNo=it.value().property("car_no").toString();
                webCarInfo.car_no=it.value().property("car_no").toString();
                check_car.car_no=carNo;
               // qDebug()<<"webCarInfo.car_no"<<webCarInfo.car_no;
            }
            if(!it.value().property("car_type").toString().isEmpty())
            {
                car_type=it.value().property("car_type").toString();
                webCarInfo.car_type=it.value().property("car_type").toString();
                check_car.car_type=webCarInfo.car_type;
               // qDebug()<<"webCarInfo.car_type"<<webCarInfo.car_type;
            }
            if(!it.value().property("person_name").toString().isEmpty())
            {
                person_name=it.value().property("person_name").toString();
                webCarInfo.person_name=it.value().property("person_name").toString();
               // qDebug()<<"webCarInfo.person_name"<<webCarInfo.person_name;
            }

            if(!it.value().property("person_tel").toString().isEmpty())
            {
                webCarInfo.person_tel=it.value().property("person_tel").toString();
               // qDebug()<<"webCarInfo.person_tel"<<webCarInfo.person_tel;
            }
            if(!it.value().property("person_address").toString().isEmpty())
            {
                webCarInfo.person_address=it.value().property("person_address").toString();
               // qDebug()<<"webCarInfo.person_address"<<webCarInfo.person_address;
            }
            if(!it.value().property("start_date").toString().isEmpty())
            {
                start_date=it.value().property("start_date").toString();//.replace(10,1," ").section('.',0,0);
                webCarInfo.start_date=it.value().property("start_date").toString();
                check_car.beginTime=webCarInfo.start_date;
              //  qDebug()<<"webCarInfo.start_date"<<webCarInfo.start_date;
            }
            if(!it.value().property("stop_date").toString().isEmpty())
            {
                stop_date=it.value().property("stop_date").toString();//.replace(10,1," ").section('.',0,0);
                webCarInfo.stop_date=it.value().property("stop_date").toString();
                check_car.endTime=webCarInfo.stop_date;
                qDebug()<<"webCarInfo.stop_date"<<webCarInfo.stop_date;
            }
            if(!it.value().property("status").toString().isEmpty())
            {
                webCarInfo.status=it.value().property("status").toString();
               // qDebug()<<"status"<<webCarInfo.status;
            }
            if(!it.value().property("fee_type").toString().isEmpty())
            {
                fee_type=it.value().property("fee_type").toString();
                webCarInfo.fee_type=it.value().property("fee_type").toString();


            }
            if(!it.value().property("begin_time").toString().isEmpty())
            {
                begin_time=it.value().property("begin_time").toString();
                webCarInfo.begin_time=it.value().property("begin_time").toString();
              //qDebug()<<"webCarInfo.begin_time"<<webCarInfo.begin_time;
            }
            if(!it.value().property("end_time").toString().isEmpty())
            {
                end_time=it.value().property("end_time").toString();
                webCarInfo.end_time=it.value().property("end_time").toString();
//                check_car.end_time=webCarInfo.end_time;

            }
            if(!it.value().property("allow_count").toString().isEmpty())
            {
                allow_count=it.value().property("allow_count").toString();
                webCarInfo.allow_count=it.value().property("allow_count").toString();
         //       check_car.=webCarInfo.allow_count;

            }
            if(!it.value().property("allow_park_time").toString().isEmpty())
            {
                allow_park_time=it.value().property("allow_park_time").toString();
                webCarInfo.allow_park_time=it.value().property("allow_park_time").toString();

            }
            if(!it.value().property("vehicle_type").toString().isEmpty())
            {
                vehicle_type=it.value().property("vehicle_type").toString();
                webCarInfo.vehicle_type=it.value().property("vehicle_type").toString();

            }
            if(!it.value().property("created_at").toString().isEmpty())
            {
                created_at=it.value().property("created_at").toString();
                webCarInfo.created_at=it.value().property("created_at").toString();

            }
            if(!it.value().property("updated_at").toString().isEmpty())
            {
                updated_at=it.value().property("updated_at").toString();
                webCarInfo.updated_at=it.value().property("updated_at").toString();


            }
            ////检查数据库固定车车辆数据
            ////固定车车辆数据更新需要修改。
            if(webCarInfo.status=="正常")
            {
                bool mes;
                mes=pt_mydb->car_chek(&pt_mydb->sqlite_db,&check_car,QString("0"),carNo);  /////???
                if(mes)
                {
                    // qDebug()<<"delete carno "<<carNo;
                    pt_mydb->car_delete(&pt_mydb->sqlite_db,carNo);
                    // qDebug()<<" car_delete ok";
                }
                usleep(5000);
                // qDebug()<< "insert to sqlite the no is :"<<webCarInfo.car_no;
                pt_mydb->writeSqliteCarweb(&pt_mydb->sqlite_db,&webCarInfo);
                //  qDebug()<<" writeSqliteCarweb";
                reply->deleteLater();
            }else if(webCarInfo.status=="作废"){
                bool mes;
                // qDebug()<<" cars do not used need to delete: ";
                mes=pt_mydb->car_chek(&pt_mydb->sqlite_db,&check_car,QString("0"),carNo);
                if(mes)
                {
                    pt_mydb->car_delete(&pt_mydb->sqlite_db,carNo);
                    if(pt_mydb->check_car_bind(&pt_mydb->sqlite_db,carNo))
                    {
                        pt_mydb->delete_car_bind(&pt_mydb->sqlite_db,carNo);
                    }
                }
                reply->deleteLater();
            }
        }
        return true;
    }else{
        emit MsgSignal(max_updated_at);
        return false;
    }
}

QString  Car_Product::cardown(QString max_updated_at)
{  
    QString str;
    str=("http://");
    str=str.append(sysInfo->serverIp).append("/api/v1/down_info_vehicle");
    str.append("?controller_sn=").append(sysInfo->galleryNumber).append("&max_updated_at=").append(max_updated_at);
  //  qDebug()<<"str"<<str;
    return str;
}

void Car_Product::receiv_Msg(QString rec_time)
{
  max_updated_at= rec_time;
}
